package com.zslibrary.StateHandle;

import android.os.SystemClock;
import android.util.Log;

import com.zslibrary.Enum.StateDefine;
import com.zslibrary.Enum.StateEventDefine;
import com.zslibrary.Pipe.Pipe;
import com.zslibrary.Protocol.Adjustment;
import com.zslibrary.Protocol.ComponentOption;
import com.zslibrary.Tools.ZSLog;

public class InitState extends StateHandle {
    private Pipe mPipe;

    public InitState(Pipe pipe){
        mPipe=pipe;
    }

    @Override
    public @StateDefine.State int getNextHop(@StateEventDefine.StateEvent int event) {
        if(event==StateEventDefine.EVT_DISCONNECT)
            return StateDefine.STATE_ERROR;
        if(event==StateEventDefine.EVT_EXECUTE)
            return StateDefine.STATE_BUSY;
        if(event==StateEventDefine.EVT_RESET)
            return StateDefine.STATE_INIT;
        return StateDefine.STATE_IDLE;
    }

    @Override
    public @StateEventDefine.StateEvent int handle(){
        ZSLog.d("init state");
        if(Adjustment.setDefaultAdjustment(mPipe)!=StateEventDefine.EVT_NONE)
            return StateEventDefine.EVT_DISCONNECT;
        ZSLog.d("set default adjustment done");
        SystemClock.sleep(300);
        if(Adjustment.setUserAdjustment(mPipe)!=StateEventDefine.EVT_NONE)
            return StateEventDefine.EVT_DISCONNECT;
        ZSLog.d("set user adjustment done");
        SystemClock.sleep(300);
        int event= ComponentOption.initComponent(mPipe);
        ZSLog.d("init component done");
        return event;
    }
}
